home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Visual Cafe Pro v1.0 / SOURCE.BIN / DefaultDataSource.java < prev    next >
Encoding:
Java Source  |  1997-06-19  |  9.3 KB  |  330 lines

  1. /*
  2.  * DefaultDataSource.java   1.0   12 Jan 1997
  3.  *
  4.  * Copyright (c) 1996 Krumel & Associates, Inc.  All Rights Reserved.
  5.  *
  6.  * This software is provided as is.  Krumel & Associates shall not be liable
  7.  * for any damages suffered by licensee as a result of using, modifying or
  8.  * distributing this software or its derivatives.
  9.  */
  10.  
  11. package symantec.itools.db.awt;
  12.  
  13. import java.awt.Point;
  14. import java.awt.Image;
  15.  
  16. public class DefaultDataSource implements DataSource {
  17.     Matrix      data = new Matrix();
  18.     Grid        view;
  19.  
  20.     boolean     useDefault = false;
  21.     boolean     autoNumber = false;
  22.     int         first;
  23.     Data        defaultValue;
  24.  
  25.     public DefaultDataSource(Grid v) {
  26.         this(v, false, null);
  27.     }
  28.  
  29.     public DefaultDataSource(Grid v, boolean useDefaults) {
  30.         this(v, useDefaults, null);
  31.     }
  32.  
  33.     public DefaultDataSource(Grid v, boolean useDefaults, Data defaultData) {
  34.         setGrid(v);
  35.         this.useDefault = useDefaults;
  36.         if (useDefaults) {
  37.             defaultValue = (defaultData != null) ?defaultData  :new DefaultData(this);
  38.         }
  39.     }
  40.  
  41.     public int rows() {
  42.         return data.rows();
  43.     }
  44.  
  45.     public int fetchAllRows() {
  46.         return data.rows();
  47.     }
  48.  
  49.     public void fetchMode(boolean manual) {}
  50.  
  51.     public Grid getView() { return view; }
  52.  
  53.     public int validDataRowRange(int top, int bottom) throws DataNotAvailable {
  54.         int rows = data.rows();
  55.  
  56.         if (rows <= top) {
  57.             throw new DataNotAvailable("Requested top=" + top + " but only " +
  58.                                        rows + " available");
  59.         }
  60.  
  61.         return Math.min(rows-1, bottom);
  62.     }
  63.  
  64.     public void setCurrentRow(int row) throws TypeNotSupported {}
  65.  
  66.     public void doAutoNumbering(boolean an) {
  67.         doAutoNumbering(an, first);
  68.     }
  69.  
  70.     public void doAutoNumbering(boolean an, int n) {
  71.         autoNumber = an;
  72.         first = n;
  73.         if (!useDefault && an == true) {
  74.             useDefault = true;
  75.             defaultValue = new ImageStringData(this);
  76.         }
  77.     }
  78.  
  79.     public void commitData() throws TypeNotSupported {}
  80.  
  81.     public void setDefaultData() {
  82.         if (defaultValue == null) {
  83.             setDefaultData(new DefaultData(this));
  84.         }
  85.     }
  86.  
  87.     public void setDefaultData(Data d) {
  88.         useDefault = true;
  89.         autoNumber = false;
  90.         defaultValue = d;
  91.     }
  92.  
  93.     public Data getDefaultData() {
  94.         if (defaultValue == null) {
  95.             throw new NullPointerException("Default data value not set");
  96.         }
  97.  
  98.         return defaultValue;
  99.     }
  100.  
  101.     public void setGrid(Grid v) {
  102.         view = v;
  103.     }
  104.  
  105.     public boolean supportsMeta() {
  106.         return false;
  107.     }
  108.  
  109.     public void setupGrid(Grid v) throws TypeNotSupported {
  110.         throw new TypeNotSupported("DefaultDataSource does not support meta information");
  111.     }
  112.  
  113.     public Data readData(int row, int col) throws DataNotAvailable {
  114.         return getData(row, col);
  115.     }
  116.  
  117.     public Data getData(Coordinate coords) throws DataNotAvailable {
  118.         return getData(coords.row, coords.col);
  119.     }
  120.  
  121.     public Data getData(int r, int c) throws DataNotAvailable {
  122.         try {
  123.             return (Data)data.elementAt(r, c);
  124.         } catch(NullPointerException e) {
  125.             if (autoNumber) {
  126.                 int state = view.rowState(r+1);
  127.                 defaultValue.setText(Integer.toString(r + first));
  128.                 if (state == DELETED_ROW) {
  129.                     defaultValue.appendChar('*');
  130.                 }
  131.                 if (view.relation != null) {
  132.                     DbDataSource source = (DbDataSource)view.dataSource;
  133.                     DbaDataStore store = (DbaDataStore)source.store;
  134.                     String text = defaultValue.toString();
  135.                     if (store.isCurrentRow(r+1)) {
  136.                         defaultValue.setText('<' + text + '>');
  137.                     }
  138.                 }
  139.                 return defaultValue;
  140.             } else if (useDefault) {
  141.                 if (defaultValue instanceof DefaultData) {
  142.                     ((DefaultData)defaultValue).setRowAndCol(r, c);
  143.                 }
  144.                 return defaultValue;
  145.             } else {
  146.                 throw e;
  147.             }
  148.         }
  149.     }
  150.  
  151.     public void setData(Coordinate coords, Data d) throws TypeNotSupported {
  152.         setData(coords.row, coords.col, d);
  153.     }
  154.  
  155.     public void setData(int r, int c, Data d) throws TypeNotSupported {
  156.         data.updateElement(r, c, d);
  157.  
  158.     }
  159.  
  160.     public String getText(Coordinate coords) throws DataNotAvailable {
  161.         return getData(coords.row, coords.col).toString();
  162.     }
  163.  
  164.     public boolean supports(Coordinate coords, int type) {
  165.         return type == Data.STRING ||
  166.                type == Data.IMAGE ||
  167.                type == Data.IMAGE_STRING;
  168.     }
  169.  
  170.     public Image getImage(Coordinate coords) throws DataNotAvailable {
  171.         Data d = (Data)data.elementAt(coords.row, coords.col);
  172.         return d.toImage();
  173.     }
  174.  
  175.     public boolean handleEvent(java.awt.Event e) {
  176.         if (e.arg instanceof TableCell) {
  177.             TableCell cell = (TableCell)e.arg;
  178.             Data data;
  179.             try {
  180.                 data = cell.getData();  //this is kind of silly and circular
  181.             } catch(DataNotAvailable ex) { return false; }
  182.  
  183.             switch(e.id) {
  184.                 case view.UNDO_CELL_EVENT:
  185.                     data.rollback();
  186.                     break;
  187.                 case view.LOST_FOCUS:
  188.                     try {
  189.                         data.commit();
  190.                     } catch(TypeNotSupported ex) {
  191.  
  192.                     }
  193.                     break;
  194.             }
  195.         }
  196.  
  197.         return false;
  198.     }
  199.  
  200.     //zero based
  201.     public void handleException(int row, int col, Exception ex) {
  202.         view.handleException(row, col, ex);
  203.     }
  204.  
  205.     public void undeleteRow(int row) throws TypeNotSupported {
  206.         throw new TypeNotSupported("Undelete not supported in DefaultDataSource");
  207.     }
  208.  
  209.     public void deleteRow(int row) throws TypeNotSupported {
  210.         data.removeRow(row);
  211.     }
  212.  
  213.     public void insertRow(int row) throws TypeNotSupported {
  214.         data.insertRow(row);
  215.     }
  216.  
  217.     public int appendRow() throws TypeNotSupported {
  218.         data.insertRow(data.rows());
  219.  
  220.         return data.rows()-1;
  221.     }
  222.  
  223.     public int rowState(int row) { return DataSource.NEW_ROW; }
  224.  
  225.     public void clear() {
  226.         data.removeAllElements();
  227.     }
  228.  
  229.     public void refresh() {}
  230.  
  231.     public void save() {}
  232.  
  233.     public void undoRow(int row) throws TypeNotSupported {}
  234.  
  235.     //returns Data object for row and col.  If not found then makes a new one
  236.     //and inserts it.
  237.     Data fetchData(int row, int col) {
  238.         if (data.contains(row, col)) {
  239.             return (Data)data.elementAt(row, col);
  240.         } else {
  241.             //need to create a Data
  242.             Data d = new ImageStringData(this);    //need to add column default support here
  243.             data.updateElement(row, col, d);
  244.             return d;
  245.         }
  246.     }
  247.  
  248.     public boolean isDataEditable(int row, int col) { return true; }
  249.  
  250.     public int type(int row, int col) {
  251.         return Data.STRING;
  252.     }
  253.  
  254.     public void rollbackCurrentData() {}
  255.  
  256.     public void rollback(int row, int col) {}
  257.  
  258.     public void commit(int row, int col) {}
  259.  
  260.     public boolean supportsChoice(int row, int col) {
  261.         return false;
  262.     }
  263.  
  264.     public Data[] getChoices(int row, int col) throws TypeNotSupported {
  265.         throw new TypeNotSupported();
  266.     }
  267.  
  268.     public void setText(int row, int col, String t) {
  269.         fetchData(row, col).setText(t);
  270.     }
  271.  
  272.     //pos is space where to be inserted (0 = first char)
  273.     public void insertChar(int row, int col, int pos, char c) {
  274.         fetchData(row, col).insertChar(pos, c);
  275.     }
  276.  
  277.     public void setText(int row, int col, char c) {
  278.         fetchData(row, col).setText(c);
  279.     }
  280.  
  281.     public void appendChar(int row, int col, char c) {
  282.         fetchData(row, col).appendChar(c);
  283.     }
  284.  
  285.     public void clearText(int row, int col) {
  286.         if (data.contains(row, col)) {
  287.             ((Data)data.elementAt(row, col)).clearText();
  288.         }
  289.     }
  290.  
  291.     public void deleteChar(int row, int col, int pos) {
  292.         if (data.contains(row, col)) {
  293.             ((Data)data.elementAt(row, col)).deleteChar(pos);
  294.         }
  295.     }
  296.  
  297.     public String subString(int row, int col, int spos, int epos) {
  298.         if (data.contains(row, col)) {
  299.             return ((Data)data.elementAt(row, col)).subString(spos, epos);
  300.         }
  301.  
  302.         throw new StringIndexOutOfBoundsException();
  303.     }
  304.  
  305.     public void setImage(int row, int col, Image i) {
  306.         fetchData(row, col).setImage(i);
  307.     }
  308.  
  309.     public String toString(int row, int col) {
  310.         if (data.contains(row, col)) {
  311.             return ((Data)data.elementAt(row, col)).toString();
  312.         } else {
  313.             return "";
  314.         }
  315.     }
  316.  
  317.     public Image toImage(int row, int col) {
  318.         if (data.contains(row, col)) {
  319.             return ((Data)data.elementAt(row, col)).toImage();
  320.         } else {
  321.             return null;
  322.         }
  323.     }
  324.  
  325.     public Object getSynchronizationObject() {
  326.         return data;
  327.     }
  328. }
  329.  
  330.